package com.jiyun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiyun.entity.Role;
import com.jiyun.entity.UserRole;
import com.jiyun.service.IRoleService;
import com.jiyun.service.IUserRoleService;
import com.jiyun.vo.RoleVo;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  控制层
 * </p>
 *
 * @author 聪哥哥
 * @since 2023-05-05
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    IRoleService roleService;

    /**
     * 添加接口
     */
    @RequestMapping("/add")
    public void addRole(@RequestBody Role role){
        role.setState("启用");
        role.setCreateTime(new Date());
        roleService.save(role);
    }

    /**
     * 修改接口
     */
    @RequestMapping("/update")
    public void updateRole(@RequestBody Role role){
        roleService.updateById(role);
    }


    @Autowired
    IUserRoleService userRoleService;
    /**
     * 单个删除接口
     */
    @RequestMapping("/deleteOne")
    public void deleteOne(@RequestParam Integer id){
        roleService.removeById(id);


        LambdaQueryWrapper<UserRole> userRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userRoleLambdaQueryWrapper.eq(UserRole::getRid,id);
        userRoleService.remove(userRoleLambdaQueryWrapper);

    }

    /**
     * 批量删除接口
     * 前台ajax参数ids传入示例：1,2,3  (英文逗号分隔的字符串)
     */
    @RequestMapping("/deleteBatch")
    public void deleteBatch(@RequestParam List<Integer> ids){
        roleService.removeByIds(ids);
    }

    /**
     * 根据id查询实体接口
     */
    @RequestMapping("/findById")
    public Role findById(@RequestParam Integer id){
        return roleService.getById(id);
    }

    /**
     * 查询所有接口
     */
    @RequestMapping("/findAll")
    public List<Role> findAll(){
        return roleService.list();
    }

    /**
     * 分页查询接口
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     */
    @RequestMapping("/findPage")
    public Page<Role> findPage(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestBody RoleVo roleVo){
        Page<Role> rolePage = new Page<>(page, pageSize);

        //拼接条件集
        LambdaQueryWrapper<Role> stationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        stationLambdaQueryWrapper.like(StringUtils.isNotBlank(roleVo.getName()), Role::getName,roleVo.getName());
        stationLambdaQueryWrapper.eq(StringUtils.isNotBlank(roleVo.getState()), Role::getState,roleVo.getState());
        stationLambdaQueryWrapper.ge(roleVo.getStart()!=null, Role::getCreateTime,roleVo.getStart());
        stationLambdaQueryWrapper.le(roleVo.getEnd()!=null, Role::getCreateTime,roleVo.getEnd());

        //按照num正序排
        stationLambdaQueryWrapper.orderByAsc(Role::getNum);
        return roleService.page(rolePage,stationLambdaQueryWrapper);
    }


    @RequestMapping("exportData")
    public void exportData(HttpServletResponse response) throws Exception{


        List<Role> userVoList = roleService.list();

        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet();

        int rowNum = 0;
        XSSFRow row = sheet.createRow(rowNum++);

        int cellNum = 0;
        XSSFCell cell = row.createCell(cellNum++);
        cell.setCellValue("ID");
        cell = row.createCell(cellNum++);
        cell.setCellValue("角色名称");
        cell = row.createCell(cellNum++);
        cell.setCellValue("顺序");
        cell = row.createCell(cellNum++);
        cell.setCellValue("状态");
        cell = row.createCell(cellNum++);
        cell.setCellValue("时间");


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (Role role : userVoList) {
            row = sheet.createRow(rowNum++);
            cellNum = 0;
            cell = row.createCell(cellNum++);
            cell.setCellValue(role.getId());
            cell = row.createCell(cellNum++);
            cell.setCellValue(role.getName());
            cell = row.createCell(cellNum++);
            cell.setCellValue(role.getNum());
            cell = row.createCell(cellNum++);
            cell.setCellValue(role.getState());
            cell = row.createCell(cellNum++);
            cell.setCellValue(sdf.format(role.getCreateTime()));

        }


        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("角色列表.xlsx", "UTF-8"));
        response.setHeader("Connection", "close");
        response.setHeader("Content-Type", "application/octet-stream");
        workbook.write(response.getOutputStream());
    }

}
